$(function () {
  $("header h4").text("品牌导航");
  $("div.more").html("");

  let shopid = 0;
  let areaid = 0;
  productRender({ shopid, areaid });

  function productRender({ shopid, areaid }) {
    $.ajax({
      url: "http://chst.vip:1234/api/getgsproduct",
      data: { shopid, areaid },
      success: (res) => {
        let htm = "";
        $.each(res.result, (i, item) => {
          htm += `
            <a href="#">
                <img
                src="${item.productImg}"
                alt=""
                />
                <span>${item.productName}/span>
                <p>${item.productPrice}</p>
            </a>`;
        });
        $("main").html(htm);
      },
    });
  }

  //店铺筛选
  $.ajax({
    url: "http://chst.vip:1234/api/getgsshop",
    success: (res) => {
      let htm = "";
      $.each(res.result, (i, item) => {
        htm += `
        <li shopid="${item.shopId}">${item.shopName}</li>
        `;
      });
      $("nav .ul1")
        .html(htm)
        .hide()
        .on("click", "li", function () {
          $("section .shopIdTitle").html(
            `<span>${$(this).text()}</span><b>▲</b>`
          );
          $(this).parent().slideToggle("fast");
          shopid = $(this).attr("shopid");
          productRender({ shopid, areaid });
        });
      $("section .shopIdTitle").click(function () {
        $("nav .ul1").slideToggle("fast");
        $(this).children("b").text("▼");
      });
    },
  });
  //地区筛选
  $.ajax({
    url: "http://chst.vip:1234/api/getgsshoparea",
    success: (res) => {
      let htm = "";
      $.each(res.result, (i, item) => {
        htm += `
        <li areaid="${item.areaId}">${item.areaName}</li>
        `;
      });
      $("nav .ul2")
        .html(htm)
        .hide()
        .on("click", "li", function () {
          $("section .areaIdTitle").html(
            `<span>${$(this).text().slice(0, 2)}</span><b>▲</b>`
          );
          $(this).parent().slideToggle("fast");
          areaid = $(this).attr("areaid");
          productRender({ shopid, areaid });
        });
      $("section .areaIdTitle").click(function () {
        $("nav .ul2").slideToggle("fast");
        $(this).children("b").text("▼");
      });
    },
  });
});
